import { createApp } from 'vue'
import axios from 'axios'
import ElementPlus from 'element-plus'  //引入element-plus库
import VForm3 from 'vform3-builds'  //引入VForm3库
import router from './router'
import store from './store'

import { Button, Dialog, Icon,
    Divider, Tab, Tabs, Toast, Notify, Tabbar, TabbarItem,
    Image as VanImage, Lazyload, Loading, Form, Field, CellGroup,Calendar, Stepper, Popover, 
    List, Cell, Tag, Search, ActionSheet, TreeSelect, Grid, GridItem, ContactCard, Popup,Picker,
    RadioGroup, Radio, Uploader , Cascader,Overlay
} from 'vant'
import VueLuckyCanvas from '@lucky-canvas/vue'

import App from './App.vue'



// import 'element-plus/dist/index.css'  //引入element-plus样式
// import 'vform3-builds/dist/designer.style.css'  //引入VForm3样式
import 'lib-flexible'
import 'vant/lib/index.css'
// import './assets/css/style.scss'

// import tcb from 'tcb-js-sdk'
import cloudbase from "@cloudbase/js-sdk";
import utils from "./router/util";


const mycloud = cloudbase.init({
        env: 'adegg-2gzf0s396d911a4a', // 需要更换为自己的环境Id，如我的是：share-195a54
        region: 'ap-guangzhou'
    })

const app = createApp(App)
axios.defaults.baseURL="http://119.91.25.158:3000/"
// axios.defaults.baseURL="http://localhost:3000/" // 开发环境
axios.interceptors.request.use(config=>{
    console.log('Main日志',config, 'TOKEN', utils.getCookie('mytoken') )
    const  token_type="Bearer"
    config.headers.Authorization =token_type+ ' '+ utils.getCookie('mytoken')
    // 在最后必须返回config
    return config
  })

  function getAuthToken() {
    //   axios.
  }

  axios.interceptors.response.use(undefined, err => {
    const error = err.response;
    // if error is 401 
    if ( (error.status===401||error.status===400) && error.config && 
    !error.config.__isRetryRequest) {
    // request for a new token
    // return getAuthToken().then(response => {
    //  // update the error config with new token
    //  error.config.__isRetryRequest = true;
    //  error.config.headers.token= localStorage.getItem("accessToken");
    //  return client(error.config);
    // });
        console.log('拦截器-返回400/401-Code', error.status)
        if (error.status === 401) {
       
            utils.clearCookie("mytoken");
            console.log('router 401 back')
            app.$router.push(`/loginSale`);
                      
        }
        return {
            status: 'error',
            code: error.status
        }
   } 
  });
  
app.config.globalProperties.$axios = axios
app.config.globalProperties.$mapp = mycloud

app.provide('$cloud', mycloud)  // 全局提供注入
app.provide('$http', axios)

app.use(Button).use(Dialog).use(Icon)
    .use(Divider).use(Tab).use(Tabs)
    .use(Toast).use(Notify).use(Tabbar).use(TabbarItem)
    .use(VanImage).use(Lazyload).use(Loading).use(Form)
    .use(Field).use(CellGroup).use(Calendar).use(Stepper).use(Popover).use(List).use(Cell).use(Tag).use(Search).use(ActionSheet)
    .use(TreeSelect).use(Grid).use(GridItem).use(ContactCard).use(Popup).use(Picker)
    .use(RadioGroup).use(Radio).use(Uploader).use(Cascader).use(Overlay)
// app.use(ElementPlus)  //全局注册element-plus
app.use(VForm3)  //全局注册VForm3(同时注册了v-form-designe、v-form-render等组件)
app.use(VueLuckyCanvas)
    
app.use(router).use(store)

app.mount('#app')


